package org.alex.samples.dao;

import java.util.List;

import org.alex.samples.model.ExamRole;
import org.alex.samples.model.ExamUserRole;
import org.alex.samples.model.Users;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
@Transactional
public class RoleDAOImp extends BaseHibernateDAO<ExamUserRole> implements IRoleDAO{

	@Override
	public ExamUserRole getUserByUserId(String userId) {
		ExamUserRole exsitsUser = this.queryForObject("FROM ExamUserRole as r where r.users.userId=?", new Object[]{userId});
		if (exsitsUser != null) {
			return exsitsUser;
		}else{
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Users> findAllStudent() {
		String hql = " SELECT e.users AS users FROM ExamUserRole AS e WHERE e.examRole.roleId=25 ";
		return this.createQuery(hql).list();
	}

	@Override
	public void addUserRole(ExamUserRole examUserRole,Users user) {	
		ExamRole r = new ExamRole();
		r.setRoleId(25);
		examUserRole.setExamRole(r);
		examUserRole.setUsers(user);		
		
		this.save(examUserRole);
		
	}

	@Override
	public void delUserRole(String userId) {
		ExamUserRole ex = this.getUserByUserId(userId);
		if(ex != null){
			this.delete(ex);
		}
	}

	

}
